package com.yu.admin.modules.app.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.admin.modules.app.mapper.OrderInfoAdminMapper;
import com.yu.admin.modules.app.pojo.OrderInfoAdmin;
import com.yu.app.common.enums.OrderStatus;
import com.yu.app.common.enums.OrderTakeType;
import com.yu.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class OrderInfoAdminService {

    @Resource
    private OrderInfoAdminMapper orderInfoAdminMapper;

    /**
     * 分页条件查询
     *
     * @param pageNo   页号
     * @param pageSize 页面大小
     * @return Page<OrderInfoAdmin>
     */
    public Page<OrderInfoAdmin> getOrderInfoAdminByPage(int pageNo, int pageSize, String orderStatus) {
        Page<OrderInfoAdmin> page = new Page<>(pageNo, pageSize);
        if (StringUtils.isEmpty(orderStatus)) {
            return orderInfoAdminMapper.selectPage(page, null);
        } else {
            return orderInfoAdminMapper.selectPage(page, new QueryWrapper<OrderInfoAdmin>()
                    .eq("order_status", orderStatus));
        }
    }


    // 进入订单的下一个步
    @Transactional(rollbackFor = Exception.class)
    public String toNextOrderStatus(String orderNo, String currentStatus) throws ServiceException {
        OrderInfoAdmin orderInfo = orderInfoAdminMapper.selectById(orderNo);
        if (OrderStatus.ENUM_has_not_pay_money.text.equals(orderInfo.getOrderStatus())) { // 未付款 请确保已经线下付款或其他方式支付
            orderInfo.setOrderStatus(OrderStatus.ENUM_on_making.text); // 修改为制作中
        } else if (OrderStatus.ENUM_on_making.text.equals(orderInfo.getOrderStatus())) { // 制作中
            if (OrderTakeType.ENUM_take_out.text.equals(orderInfo.getTakeType())) { // 外卖配送方式
                orderInfo.setOrderStatus(OrderStatus.ENUM_on_sending.text); // 修改为配送中
            } else {
                orderInfo.setOrderStatus(OrderStatus.ENUM_please_take_meal.text); // 修改为请取餐
            }
            orderInfo.setFinishTime(new Date()); // 完成制作的时间
        } else if (OrderStatus.ENUM_on_sending.text.equals(orderInfo.getOrderStatus())) { // 配送中
            orderInfo.setOrderStatus(OrderStatus.ENUM_has_received.text); // 修改为已送达
            orderInfo.setTakeMealTime(new Date());
        } else if (OrderStatus.ENUM_please_take_meal.text.equals(orderInfo.getOrderStatus())) { // 请取餐
            orderInfo.setOrderStatus(OrderStatus.ENUM_has_received.text); // 修改为已送达
            orderInfo.setTakeMealTime(new Date());
        } else if (OrderStatus.ENUM_has_received.text.equals(orderInfo.getOrderStatus())) { // 已送达
            orderInfo.setOrderStatus(OrderStatus.ENUM_has_completed.text); // 完成订单
        } else {
            throw ServiceException.CONST_current_order_status_can_not_change;
        }
        orderInfoAdminMapper.updateById(orderInfo);
        return orderInfo.getOrderStatus();
    }
}
